//
//  AppDelegate.m
//  WChat
//
//  Created by lee on 2018/7/27.
//  Copyright © 2018年 mjsfax. All rights reserved.
//

#import "AppDelegate.h"
#import "EaseMob.h"
#import "TabbarViewController.h"
#import "LoginController.h"
#import "ContentViewController.h"

@interface AppDelegate ()<EMChatManagerDelegate>
@property (strong, nonatomic) TabbarViewController *tabbar;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    [[EaseMob sharedInstance] registerSDKWithAppKey:kEaseMobAppKey apnsCertName:nil otherConfig:@{kSDKConfigEnableConsoleLogger:@NO}];
    [[EaseMob sharedInstance].chatManager addDelegate:self delegateQueue:nil];
    
    [[EaseMob sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
    
    if ([[EaseMob sharedInstance].chatManager isAutoLoginEnabled]) {
        [MSToast show:@"自动登录中。。。"];
    }
    
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
  
}

//进入后台
- (void)applicationDidEnterBackground:(UIApplication *)application {
    [[EaseMob sharedInstance] applicationDidEnterBackground:application];
}

//从后台返回
- (void)applicationWillEnterForeground:(UIApplication *)application {
    [[EaseMob sharedInstance] applicationWillEnterForeground:application];
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
   
}
//app将销毁
- (void)applicationWillTerminate:(UIApplication *)application {
    [[EaseMob sharedInstance] applicationWillTerminate:application];
    [[EaseMob sharedInstance].chatManager removeDelegate:self];
    
}

#pragma mark - 开启自动登录后 再次启动会自动登录回调
- (void)willAutoLoginWithInfo:(NSDictionary *)loginInfo error:(EMError *)error {
    if (error) {
        NSLog(@"%@",error);
        return;
    }
    NSLog(@"将要自动登录%@",loginInfo);
}

- (void)didAutoLoginWithInfo:(NSDictionary *)loginInfo error:(EMError *)error {
    if (error) {
        NSLog(@"%@",error);
        [MSToast show:@"自动登录失败"];
        return;
    }
    NSLog(@"完成自动登录%@",loginInfo);
    
    ContentViewController *content = [[ContentViewController alloc] init];
    self.tabbar = [TabbarViewController initWithParentController:content];
    self.window.rootViewController = content;
}

#pragma mark - delegate 掉线时ios sdk会自动重连
- (void)willAutoReconnect {
    NSLog(@"将要发起自动重连%@",__func__);
}

- (void)didAutoReconnectFinishedWithError:(NSError *)error {
    if (error) {
        NSLog(@"%@",error);
        return;
    }
    NSLog(@"自动重连完成%@",__func__);
    [MSToast show:@"自动重连完成"];
}

#pragma mark - 被踢

- (void)didRemovedFromServer {
    NSLog(@"被踢了%@",__func__);
}

- (void)didLoginFromOtherDevice {
    NSLog(@"被踢了%@",__func__);
}

- (void)didLogoff {
    [[EaseMob sharedInstance].chatManager asyncLogoffWithUnbindDeviceToken:NO completion:^(NSDictionary *info, EMError *error) {
        if (error) {
            NSLog(@"%@",error);
        } else {
            self.window.rootViewController = [[LoginController alloc] init];
        }
    } onQueue:nil];
}

#pragma mark - public

- (UINavigationController *)getNavigationController {
    if (!self.tabbar) {
        self.tabbar = [[[UIApplication sharedApplication].keyWindow.rootViewController childViewControllers] firstObject];
    }
    self.tabbar = (TabbarViewController *)self.window.rootViewController;
    return (UINavigationController *)[self.tabbar selectedViewController];
}

@end
